package com.example.myapplication;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class TransactionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final List<TransactionGroup> transactionGroupList;
    private final Context context;

    private static final int TYPE_HEADER = 0;
    private static final int TYPE_ITEM = 1;

    public TransactionAdapter(List<TransactionGroup> transactionGroupList, Context context) {
        this.transactionGroupList = transactionGroupList;
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        // 判断当前项是标题还是交易项
        if (transactionGroupList.get(position) instanceof TransactionGroup) {
            return TYPE_HEADER; // 状态标题
        } else {
            return TYPE_ITEM; // 交易项
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_transaction_header, parent, false);
            return new HeaderViewHolder(view);
        } else {
            System.out.println("qqqq  onCreateViewHolder");
            View view = LayoutInflater.from(context).inflate(R.layout.item_transaction, parent, false);
            return new TransactionViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 获取当前的 TransactionGroup
        TransactionGroup transactionGroup = transactionGroupList.get(position);

        if (holder instanceof HeaderViewHolder) {
            // 绑定标题
            String status = transactionGroup.getStatus();
            ((HeaderViewHolder) holder).tvStatus.setText(status);
        } else if (holder instanceof TransactionViewHolder) {
            // 绑定交易项
            // 需要根据位置找到对应的交易项
            int groupPosition = findGroupPosition(position);
            Transaction transaction = transactionGroup.getTransactions().get(groupPosition);
            ((TransactionViewHolder) holder).tvProductName.setText(transaction.getProductName());
            ((TransactionViewHolder) holder).tvBuyerSeller.setText("买家ID: " + transaction.getBuyerId() + " | 卖家ID: " + transaction.getSellerId());
            ((TransactionViewHolder) holder).tvStatus.setText("状态: " + transaction.getStatus());
            ((TransactionViewHolder) holder).tvDate.setText("交易时间: " + transaction.getDate());

            holder.itemView.setOnClickListener(v -> {
                if (transaction.getStatus().equals("待支付")) {
                    Intent intent = new Intent(context, ScanPayActivity.class);
                    context.startActivity(intent);
                } else if (transaction.getStatus().equals("已完成")) {
                    Intent intent = new Intent(context, ConfirmReceiptActivity.class);
                    intent.putExtra("transaction_id", transaction.getTransactionId());
                    intent.putExtra("product_name", transaction.getProductName());
                    intent.putExtra("purchase_date", transaction.getDate());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return transactionGroupList.size();
    }

    // 计算当前交易项在其组中的位置
    private int findGroupPosition(int position) {
        int groupPosition = 0;
        for (int i = 0; i < position; i++) {
            if (transactionGroupList.get(i) instanceof TransactionGroup) {
                groupPosition += ((TransactionGroup) transactionGroupList.get(i)).getTransactions().size();
            }
        }
        return position - groupPosition - 1; // 减去标题的数量
    }

    public static class HeaderViewHolder extends RecyclerView.ViewHolder {
        TextView tvStatus;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
            tvStatus = itemView.findViewById(R.id.tvStatus);
        }
    }

    public static class TransactionViewHolder extends RecyclerView.ViewHolder {
        TextView tvProductName, tvBuyerSeller, tvStatus, tvDate;

        public TransactionViewHolder(@NonNull View itemView) {
            super(itemView);
            tvProductName = itemView.findViewById(R.id.tvProductName);
            tvBuyerSeller = itemView.findViewById(R.id.tvBuyerSeller);
            tvStatus = itemView.findViewById(R.id.tvStatus);
            tvDate = itemView.findViewById(R.id.tvDate);
        }
    }
}